<?php
class home extends CI_Controller {
	
	function home() {
		parent::__construct ();
		$this->load->model ( 'fe/home_model', 'model' );
		$this->load->model ( 'be/support_model' );
		$this->load->model ( 'fe/fe_loader', 'loader' );
		$this->load->helper ( 'form', 'url' );
	}
	
	public function loadCategories() {
		$categories = $this->loader->loadTable ( 'category' );
		$frm_cate_home = '<ul>';
		foreach ( $categories as $cate ) {
//			<input type="checkbox" name="' . $cate ['id'] . '" value="' . $cate ['id'] . '">' . $cate ['categoryName'] . '</input> 
			$frm_cate_home .= '<li>
				<a href="'.base_url().'index.php/home/loadProvidersByCate/'.$cate['id'].'">' . $cate ['categoryName'] . '</a>
			</li>';
		}
		return $frm_cate_home . '</ul>';
	}
    
	public function loadPricePolicies() {
		$pprice = $this->loader->loadTable ( 'pricepolicy' );
		$frm_pprice_home = '<ul>';
		foreach ( $pprice as $price ) {
			$frm_pprice_home .= '<li>
				<a href="'.base_url().'index.php/home/loadProvidersByPrice/'.$price['id'].'">' . $price ['value'] . '.000 VNĐ</a>
			</li>';
		}
		return $frm_pprice_home . '</ul>';
	}
	
	public function loadDistricts(){
		$districts = array('Ba Đình', 'Đống Đa', 'Cầu Giấy', 'Hoàn Kiếm', 'Thanh Xuân', 'Long Biên', 'Hoàng Mai');
		$frm_district_home = '<ul>';
		foreach ($districts as $d){
			$frm_district_home .= '<li><a href="'.base_url().'index.php/home/loadProvidersByDistrict/'.$d.'">' . $d . '</a></li>';
		}
		return $frm_district_home;
	}
	
	public function loadProviders() {
		return $this->loader->loadTable ( 'provider' );
	}
	
	public function getProvider($id) {
		return $this->loader->loadTable ( 'provider', $id );
	}
	
	public function loadProvidersByCate($cateId){
		$data = $this->initDataArray();
		$data ['main_content'] = 'fe/home';
		$data ['page_title'] = 'Trang chủ';
		$data ['providers'] = $this->model->loadProvidersByCate($cateId);
//		print_r($data['providers']);
		$this->load->view ( 'fe/includes/template', $data );
	}
	
	public function loadProvidersByPrice($priceId){
		$data = $this->initDataArray();
		$data ['main_content'] = 'fe/home';
		$data ['page_title'] = 'Trang chủ';
		$data ['providers'] = $this->model->loadProvidersByPrice($priceId);
		$this->load->view ( 'fe/includes/template', $data );
	}
	
	public function loadProvidersByDistrict($district){
		$data = $this->initDataArray();
		$data ['main_content'] = 'fe/home';
		$data ['page_title'] = 'Trang chủ';
		$data ['providers'] = $this->model->loadProvidersByDistrict($district);
		$this->load->view ( 'fe/includes/template', $data );
	}
	
	function loadNewsById($newsId){
		$data = $this->initDataArray();
		$data ['main_content'] = 'fe/news';
		$data ['page_title'] = 'Tin tức';
		$data ['news'] = $this->model->loadNewsById($newsId);
		$this->load->view ( 'fe/includes/template', $data );
	}
	
	function index() {
		$this->default_view ();
	}
	
	function default_view() {
		$data = $this->initDataArray();
		$data ['main_content'] = 'fe/home';
		$data ['page_title'] = 'Trang chủ';
		$data ['providers'] = $this->loadProviders ();
		$this->load->view ( 'fe/includes/template', $data );
	}
	
	function get_price_icons($provider_id) {
		$result = $this->model->get_price_icons($provider_id);
		$icons = array();
		$index = 0; 
		foreach ($result as $item) {
			$icons[$index] = '<img src="' . base_url() . $item . '" />';
			//echo $icons[$index];
			$index++;
		}
	}
	
	function get_time_icons($provider_id) {
		$result = $this->model->get_time_icons($provider_id);
		$icons = array();
		$index = 0; 
		foreach ($result as $item) {
			$icons[$index] = '<img src="' . base_url() . $item . '" />';
			//echo $icons[$index];
			$index++;
		}
		
	}
	
	function initDataArray() {
		// Slide
		$data ['slide'] = $this->model->loadSlide ();
		// Support
		$data ['skype'] = $this->support_model->get_by_type ( 'Skype' );
		$data ['yahoo'] = $this->support_model->get_by_type ( 'Yahoo' );
		$data ['hotline'] = $this->support_model->get_by_type ( 'Hotline' );
		$data ['email'] = $this->support_model->get_by_type ( 'Email' );
		// Left column
		$data ['categories'] = $this->loadCategories ();
		$data ['price_policies'] = $this->loadPricePolicies ();
		$data ['districts'] = $this->loadDistricts();
		
		// News
		 $data ['news'] = $this->model->loadNews();
		return $data;
	}
	
	function best_providers() {
		$data = $this->initDataArray();
		$data['providers'] = $this->model->getProviders();
		$data['page_title'] = 'Tốt nhất';
		$data['main_content'] = 'fe/best_view';
		$data ['page_title'] = 'Trang chủ';
		$this->load->view('fe/includes/template', $data);
	}
	
	function provider_detail($id) {
		$data ['categories'] = $this->loader->loadCategories ();
		$data ['price_policies'] = $this->loader->loadPricePolicies ();
		$data ['providers'] = $this->loader->loadProviders ();
		$data ['provider'] = $this->loader->getProvider ( $id );
		
		$data ['page_title'] = 'CARDnet Online ';
		$data ['main_content'] = 'fe/provider_detail';
		$this->load->view ( 'fe/includes/template', $data );
	}
}